package com.hongdee.base.maint.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.base.maint.entity.TBcMaintantType;
import com.hongdee.base.maint.service.IMaintantTypeService;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by My on 2017/2/15.
 */
@RestController
@RequestMapping("/base/maint")
public class MaintantTypeController {
    @Autowired
    private IMaintantTypeService iMaintantTypeService;
    /**
     * @param  uid 登录用户id
     * @param  token 登录用户 token
     * @param  maintPCode 维修大类编码
     * @return Map
     * @throws Exception
     * @desc   分页查询
     * @author 徐钦勇
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "maintPCode", defaultValue = "") String maintPCode) throws Exception {
        Map map= iMaintantTypeService.page(uid,start, limit,maintPCode);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
    /**
     * @param  uid 登录用户id
     * @param  token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   维修类别明细
     * @author 徐钦勇
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = this.iMaintantTypeService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc    启用start
     * @author 徐钦勇
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        this.iMaintantTypeService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   停用stop
     * @author 徐钦勇
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        this.iMaintantTypeService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return ResultEntity
     * @throws Exception
     * @desc   车系保存 Save
     * @author 徐钦勇
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TBcMaintantType data) throws Exception{
        EntityUtils.valid(data);
        String oid= TokenUtil.getCompanyId(uid);
        data.setCompanyId(oid);
        TBcMaintantType tBcCar=this.iMaintantTypeService.saveOrUpdate(data);
        ResultEntity res=new ResultEntity();
        res.setData(tBcCar);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }
}
